
package com.bear.cmcc.NetTestHTTP.httpUtil;

import com.bear.cmcc.NetTestHTTP.component.CityBean;
import com.bear.cmcc.NetTestHTTP.component.json.ColumnList;
import com.bear.cmcc.NetTestHTTP.component.json.Columns;
import com.bear.cmcc.NetTestHTTP.component.json.request.AppUpdateRequest;
import com.bear.cmcc.NetTestHTTP.component.json.request.CellInfoRequest;
import com.bear.cmcc.NetTestHTTP.component.json.request.CityAreaFtpInfoRequest;
import com.bear.cmcc.NetTestHTTP.component.json.request.CityGridRequest;
import com.bear.cmcc.NetTestHTTP.component.json.request.CityInfoRequest;
import com.bear.cmcc.NetTestHTTP.component.json.request.CityWlanRequest;
import com.bear.cmcc.NetTestHTTP.component.json.request.ColumnDetailRequest;
import com.bear.cmcc.NetTestHTTP.component.json.request.ColumnListRequest;
import com.bear.cmcc.NetTestHTTP.component.json.request.CountryCityFtpInfoRankRequest;
import com.bear.cmcc.NetTestHTTP.component.json.request.GridFtpInfoRequest;
import com.bear.cmcc.NetTestHTTP.component.json.request.LoginRequest;
import com.bear.cmcc.NetTestHTTP.component.json.request.NetDataDescriInfoRequest;
import com.bear.cmcc.NetTestHTTP.component.json.request.SelfRankRequest;
import com.bear.cmcc.NetTestHTTP.component.json.request.SubmitTelNumberRequest;
import com.bear.cmcc.NetTestHTTP.component.json.request.SubmitTestinfoRequest;
import com.bear.cmcc.NetTestHTTP.component.json.respond.AppUpdateRespond;
import com.bear.cmcc.NetTestHTTP.component.json.respond.CityAreaFtpInfoRespond;
import com.bear.cmcc.NetTestHTTP.component.json.respond.CityDataInfoRespond;
import com.bear.cmcc.NetTestHTTP.component.json.respond.CityGridRespond;
import com.bear.cmcc.NetTestHTTP.component.json.respond.CityInfoRespond;
import com.bear.cmcc.NetTestHTTP.component.json.respond.CityWlanRespond;
import com.bear.cmcc.NetTestHTTP.component.json.respond.ColumnDetailRespond;
import com.bear.cmcc.NetTestHTTP.component.json.respond.ColumnListRespond;
import com.bear.cmcc.NetTestHTTP.component.json.respond.ColumnsRespond;
import com.bear.cmcc.NetTestHTTP.component.json.respond.CountryCityFtpInfoRankRespond;
import com.bear.cmcc.NetTestHTTP.component.json.respond.CountryInfoRespond;
import com.bear.cmcc.NetTestHTTP.component.json.respond.GetNetDataReturnInfoRespond;
import com.bear.cmcc.NetTestHTTP.component.json.respond.LoginRespond;
import com.bear.cmcc.NetTestHTTP.component.json.respond.SelfRankRespond;
import com.bear.cmcc.NetTestHTTP.component.json.respond.ServiceConfigRespond;
import com.bear.cmcc.NetTestHTTP.component.json.respond.SubmitTelNumberRespond;
import com.bear.cmcc.NetTestHTTP.component.json.respond.SubmitTestinfoRespond;
import com.bear.cmcc.NetTestHTTP.constant.HttpConstant;
import com.bear.cmcc.NetTestHTTP.util.MyCommonUtil;
import com.bear.net.httpclient.HttpRequest;
import com.bear.net.httpclient.util.MyDes;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

import org.apache.http.HttpStatus;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.os.Message;
import android.telephony.TelephonyManager;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;

/**
 * 这个接口主要是和http请求相关。
 * 
 * @author suchangjun
 */
public class HttpUtil {
    public final static String BASE_URI = "http://218.207.217.123:8093/PhoneHelper";

    /**
     * 获取session_id
     */
    public final static String GET_SESSION_ID = "getSessionId";

    /**
     * 获取总体概述情况。
     */
    public final static String GET_REGION_NET_INFO = "getRegionNetInfo";

    /**
     * 查询全国概述情况。
     */
    public final static int QUERY_ALL = 1;

    /**
     * 查询省概述情况
     */
    public final static int QUERY_PROVINCE = 2;

    /**
     * 查询城市概述情况
     */
    public final static int QUERY_CITY = 3;

    /**
     * 获取小区总体概述情况。
     */
    public final static String GET_GRID_NET_INFO = "getGridNetInfo";

    /**
     * 获取全国各省份FTP下载速度情况。
     */
    public final static String GET_COUNTRY_FTP_INFO = "getCountryFtpInfo";

    /**
     * 获取省份FTP下载速度情况。
     */
    public final static String GET_PROVINCE_FTP_INFO = "getProvinceFtpInfo";

    /**
     * 获取某市各WLAN热点FTP下载速度(只支持WLAN)。
     */
    public final static String GET_CITY_WLAN_FTP_INFO = "getCityWLANFtpInfo";

    /**
     * 09获取某网格内各小区FTP下载速度情况
     */
    public final static String GET_GRID_FTP_INFO = "getGridFtpInfo";

    /** 10获取某市内各小区FTP下载速度情况 */
    public final static String GET_CITY_AREA_FTP_INFO = "getCityAreaFtpInfo";

    /** 11获取全国各城市FTP下载速率排名 */
    public final static String GET_CITY_FTP_INFO_RANK = "getCountryCityFtpInfoRank";

    /** 12获取可订阅信息栏目列表 13 获取具体栏目最新信息列表 */
    public final static String GET_COLUMN_LIST = "getColumnList";

    public final static String GET_LATEST_COLUMN_LIST = "getLatestColumnList";

    /** 14 获取栏目列表详情信息 */
    public final static String GET_COLUMN_DETAIL = "getColumnDetail";

    /** 15提交测试报告 */
    public final static String GET_SUBMIT_TEST_INFO = "submitTestinfo";

    /** 16应用版本更新 */
    public final static String GET_APP_UPDATA = "appUpdate";

    /** 17 手机号上传 */
    public final static String GET_SUBMIT_TELNUMBER = "submitTelNumber";

    /** 18获取本机终端排名情况 */
    public final static String GET_SELFRANK = "getSelfRank";

    /** 19获取服务器配置 */
    public final static String GET_SERVICE_CONFIG = "getServiceConfig";

    /** 获取某个城市下面的网格数据 **/
    public final static String GET_CITY_GRID = "getCityFtpInfo";

    /** 登录 **/
    public final static String LOGIN = "login";

    // /**
    // * 向服务器请求获取sessionId
    // *
    // * @param context
    // * @return
    // */
    // public static SessionInfoRespond getSessionId(Context context) {
    // SessionInfoRespond sessionInfo = null;
    // try {
    // String deviceId = MyCommonUtil.getDeviceId(context);
    // if (deviceId != null) {
    // DeviceInfoRequest info = new DeviceInfoRequest(deviceId);
    // Gson gson = new GsonBuilder().setFieldNamingPolicy(
    // FieldNamingPolicy.UPPER_CAMEL_CASE).create();
    // String requestPackage = gson.toJson(info);
    // String
    // imdiMd5=MyCommonUtil.getDeviceByMD5(MyCommonUtil.getDeviceId(context));
    // Message message = HttpRequest.doPost(BASE_URI + File.separator +
    // GET_SESSION_ID,
    // requestPackage,imdiMd5);
    // if (message != null && HttpStatus.SC_OK == message.arg1) {
    // Gson gsonf = new Gson();
    // sessionInfo = gsonf.fromJson(message.obj.toString(),
    // SessionInfoRespond.class);
    // }
    // }
    // } catch (Exception e) {
    // e.printStackTrace();
    // }
    // return sessionInfo;
    // }

    /**
     * 获取网络概述情况（全国，省，市）。
     * 
     * @param context
     * @param queryAreaLevel QUERY_ALL QUERY_PROVINCE QUERY_CITY
     * @param areaCode 省市编码参照商务终端省市编码;查询全国时地区编码为空值。
     * @return NetDataDescriInfo 有可能为null。
     */
    public static GetNetDataReturnInfoRespond getRegionNetInfo(Context context, int queryAreaLevel,
            String areaCode) {
        GetNetDataReturnInfoRespond returnObject = null;
        try {
            NetDataDescriInfoRequest request = new NetDataDescriInfoRequest(queryAreaLevel,
                    areaCode);

            Gson gson = new GsonBuilder().create();
            String requestPackage = gson.toJson(request);
            String imdiMd5 = MyCommonUtil.getDeviceByMD5(MyCommonUtil.getDeviceId(context));
            Message message = HttpRequest.doPost(BASE_URI + File.separator + GET_REGION_NET_INFO,
                    requestPackage, imdiMd5);
            if (message != null && HttpStatus.SC_OK == message.arg1) {
                Gson gsonf = new Gson();
                String returnStr = message.obj.toString();
                // 测试
                // String returnStr =
                // "{ \"errorcode\":0,\"Data\":{\"averageSpeed2G\":\"35.34\",\"averageSpeedTD\": \"100.34\",\"averageSpeedWLAN\":\"500.34\",\"online2G\":12000,\"onlineTD\":12000,\"TDstayRate\":\"30%\",\"msg\":\"网优信息\"}}";
                returnObject = gsonf.fromJson(returnStr, GetNetDataReturnInfoRespond.class);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return returnObject;
    }

    /**
     * 获取小区概述情况。
     * 
     * @param context
     * @param cityCode 城市代码。
     * @param gridCode 小区代码。
     * @return GetNetDataReturnInfo 对象 有可能是null。
     */
    public static GetNetDataReturnInfoRespond getGridNetInfo(Context context,
            String cityCode, String gridCode) {
        GetNetDataReturnInfoRespond returnObject = null;
        try {
            CellInfoRequest request = new CellInfoRequest( cityCode, gridCode);

            Gson gson = new GsonBuilder().create();
            String requestPackage = gson.toJson(request);
            String imdiMd5 = MyCommonUtil.getDeviceByMD5(MyCommonUtil.getDeviceId(context));
            Message message = HttpRequest.doPost(BASE_URI + File.separator + GET_GRID_NET_INFO,
                    requestPackage, imdiMd5);
            if (message != null && HttpStatus.SC_OK == message.arg1) {
                Gson gsonf = new Gson();
                String returnStr = message.obj.toString();
                // 测试
                // String returnStr =
                // "{ \"errorcode\":0,\"Data\":{\"averageSpeed2G\":\"35.34\",\"averageSpeedTD\": \"100.34\",\"averageSpeedWLAN\":\"500.34\",\"online2G\":12000,\"onlineTD\":12000,\"TDstayRate\":\"30%\",\"msg\":\"网优信息\"}}";
                returnObject = gsonf.fromJson(returnStr, GetNetDataReturnInfoRespond.class);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return returnObject;
    }

    /**
     * 获取全国各省份FTP下载速度情况。
     * 
     * @param context
     * @return CountryInfoRespond 对象 有可能是null。
     */
    public static CountryInfoRespond getCountryFtpInfo(Context context) {
        CountryInfoRespond returnObject = null;

        try {
            String imdiMd5 = MyCommonUtil.getDeviceByMD5(MyCommonUtil.getDeviceId(context));
            Message message = HttpRequest.doPost(BASE_URI + File.separator + GET_COUNTRY_FTP_INFO,
                    null, imdiMd5);
            if (message != null && HttpStatus.SC_OK == message.arg1) {
                Gson gsonf = new Gson();
                String returnStr = message.obj.toString();
                returnObject = gsonf.fromJson(returnStr, CountryInfoRespond.class);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return returnObject;
    }

    /**
     * 获取某省份各地市FTP下载速度情况
     * 
     * @param context
     * @param provinceCode 省份代码
     * @param netType 网络类型 1代表2G，2代表TD，3代表WLAN
     * @return CountryInfoRespond 有可能为null
     */
    public static CityInfoRespond getProvinceFtpInfo(Context context, String provinceCode,
            int netType) {
        CityInfoRespond returnObject = null;

        try {
            CityInfoRequest request = new CityInfoRequest(provinceCode, netType);
            Gson gson = new GsonBuilder().create();
            String requestPackage = gson.toJson(request);
            String imdiMd5 = MyCommonUtil.getDeviceByMD5(MyCommonUtil.getDeviceId(context));
            Message message = HttpRequest.doPost(BASE_URI + File.separator + GET_PROVINCE_FTP_INFO,
                    requestPackage, imdiMd5);
            if (message != null && HttpStatus.SC_OK == message.arg1) {
                Gson gsonf = new Gson();
                String returnStr = message.obj.toString();
                returnObject = gsonf.fromJson(returnStr, CityInfoRespond.class);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return returnObject;
    }

    /**
     * 获取某省份各地市FTP下载速度情况
     * 
     * @param context
     * @param provinceCode 省份代码
     * @param netType 网络类型 1代表2G，2代表TD，3代表WLAN
     *            (MyCommonConstant.NET_TYPE.NET_TYPE_2G_INT)
     * @return CountryInfoRespond 有可能为null
     */
    public static ArrayList<CityBean> getProvinceFtpInfoAdapter(Context context,
            String provinceCode, int netType) {
        ArrayList<CityBean> result = new ArrayList<CityBean>();
        try {
            CityBean temp = null;
            CityInfoRespond respond = getProvinceFtpInfo(context, provinceCode, netType);
            if (respond != null && respond.getErrorCode() == HttpConstant.CODE_SUCCESS) {
                ArrayList<CityDataInfoRespond> responds = respond.getCitys();
                if (responds != null && responds.size() > 0) {
                    for (CityDataInfoRespond element : responds) {
                        temp = new CityBean();
                        temp.setCity_code(element.getCityCode());
                        temp.setCity_name(element.getCityName());
                        temp.setComRate(element.getConnectionRate());
                        temp.setDownSpeed((int) element.getAverageSpeed());
                        temp.setDroppingRate(element.getFailRate());
                        temp.setSignalStrength(element.getSignalStrength());
                        result.add(temp);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;

    }

    /**
     * 获取某省份各地市FTP下载速度情况
     * 
     * @param context
     * @param cityCode 城市代码
     * @return CityWlanRespond 有可能为null
     */
    public static CityWlanRespond getCityWLANFtpInfo(Context context, String cityCode) {
        CityWlanRespond returnObject = null;

        try {
            CityWlanRequest request = new CityWlanRequest(cityCode);
            Gson gson = new GsonBuilder().create();
            String requestPackage = gson.toJson(request);
            String imdiMd5 = MyCommonUtil.getDeviceByMD5(MyCommonUtil.getDeviceId(context));
            Message message = HttpRequest.doPost(
                    BASE_URI + File.separator + GET_CITY_WLAN_FTP_INFO, requestPackage, imdiMd5);
            if (message != null && HttpStatus.SC_OK == message.arg1) {
                Gson gsonf = new Gson();
                String returnStr = message.obj.toString();
                returnObject = gsonf.fromJson(returnStr, CityWlanRespond.class);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return returnObject;
    }

    /**
     * 09获取某网格内各小区FTP下载速度情况
     * 
     * @param context
     * @param provinceCode 省编码参照商务终端省编码
     * @param cityCode 市编码参照商务终端市编码
     * @param gridCode 网格编码(请服务端人员补充相关编码例子)
     * @param netType 网络类型，1代表2G，2代表TD,3代表WLAN
     * @param orderType 1代表按城市下载速率从高到低获取数据 2代表按城市下载速率从低到高获取数据
     * @param pageIndex 0表示取第1页，1表示取第2页，以此类推
     * @param pageSize 每页的数据量，例如10代表 每页10条记录
     * @return
     */
    public static CityAreaFtpInfoRespond getGridFtpInfo(Context context, GridFtpInfoRequest request) {
        CityAreaFtpInfoRespond returnObject = null;
        String returnStr = requsetUtil(request, GET_GRID_FTP_INFO, context);
        if (!returnStr.equals("")) {
            Gson gsonf = new Gson();

            try {
                returnObject = gsonf.fromJson(returnStr, CityAreaFtpInfoRespond.class);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return returnObject;
    }

    /**
     * 10获取某市内各小区FTP下载速度情况
     * 
     * @param context
     * @param cityCode 市编码参照商务终端市编码
     * @param netType 网络类型，1代表2G，2代表TD,3代表WLAN
     * @param orderType 1代表按城市下载速率从高到低获取数据 2代表按城市下载速率从低到高获取数据
     * @param pageIndex 0表示取第1页，1表示取第2页，以此类推
     * @param pageSize 每页的数据量，例如10代表 每页10条记录
     * @return
     */
    public static CityAreaFtpInfoRespond getCityAreaFtpInfo(Context context, String cityCode,
            int netType, int orderType, int pageIndex, int pageSize) {
        CityAreaFtpInfoRespond returnObject = null;
        CityAreaFtpInfoRequest request = new CityAreaFtpInfoRequest(cityCode, netType, orderType,
                pageIndex, pageSize);

        String returnStr = requsetUtil(request, GET_CITY_AREA_FTP_INFO, context);
        if (!returnStr.equals("")) {
            Gson gsonf = new Gson();

            try {
                returnObject = gsonf.fromJson(returnStr, CityAreaFtpInfoRespond.class);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return returnObject;
    }

    /**
     * 11获取全国各城市FTP下载速率排名
     * 
     * @param context
     * @param netType 网络类型，1代表2G，2代表TD,3代表WLAN
     * @param orderType 1代表按城市下载速率从高到低获取数据 2代表按城市下载速率从低到高获取数据
     * @param pageIndex 0表示取第1页，1表示取第2页，以此类推
     * @param pageSize 每页的数据量，例如10代表 每页10条记录
     * @return
     */
    public static CountryCityFtpInfoRankRespond getCountryCityFtpInfoRank(Context context,
            int netType, int orderType, int pageIndex, int pageSize) {
        CountryCityFtpInfoRankRespond returnObject = null;
        CountryCityFtpInfoRankRequest request = new CountryCityFtpInfoRankRequest(netType,
                orderType, pageIndex, pageSize);

        String returnStr = requsetUtil(request, GET_CITY_FTP_INFO_RANK, context);
        if (!returnStr.equals("")) {
            Gson gsonf = new Gson();

            try {
                returnObject = gsonf.fromJson(returnStr, CountryCityFtpInfoRankRespond.class);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return returnObject;
    }

    /**
     * 12获取可订阅信息栏目列表
     * 
     * @param context
     * @return
     */
    public static ColumnsRespond getColumnS(Context context) {
        ColumnsRespond returnObject = null;

        String returnStr = requsetUtil(null, GET_COLUMN_LIST, context);
        if (!returnStr.equals("")) {
            Gson gsonf = new Gson();

            try {
                returnObject = gsonf.fromJson(returnStr, ColumnsRespond.class);
                List<Columns> item = gsonf.fromJson(getData(returnStr, RequMsg),
                        new TypeToken<List<Columns>>() {
                        }.getType());
                returnObject.setColumns(item);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return returnObject;
    }

    /**
     * 13 获取具体栏目最新信息列表
     * 
     * @param context
     * @param columnCode 栏目编号
     * @param lastUpdateTime 上次更新时间 如果为空则表示首次获取
     * @return
     */
    public static ColumnListRespond getLatestColumnList(Context context, String columnCode,
            String lastUpdateTime) {
        ColumnListRespond returnObject = null;
        ColumnListRequest request = new ColumnListRequest(columnCode, lastUpdateTime);

        String returnStr = requsetUtil(request, GET_LATEST_COLUMN_LIST, context);
        if (!returnStr.equals("")) {
            Gson gsonf = new Gson();

            try {
                returnObject = gsonf.fromJson(returnStr, ColumnListRespond.class);
                List<ColumnList> item = gsonf.fromJson(getData(returnStr, RequMsg),
                        new TypeToken<List<ColumnList>>() {
                        }.getType());
                returnObject.setColumnInfoList(item);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return returnObject;
    }

    /**
     * 14 获取栏目列表详情信息
     * 
     * @param context
     * @param infoCode 信息编码
     * @return
     */
    public static ColumnDetailRespond getColumnDetail(Context context, String infoCode) {
        ColumnDetailRespond returnObject = null;
        ColumnDetailRequest request = new ColumnDetailRequest(infoCode);

        String returnStr = requsetUtil(request, GET_COLUMN_DETAIL, context);
        if (!returnStr.equals("")) {
            Gson gsonf = new Gson();

            try {
                returnObject = gsonf.fromJson(returnStr, ColumnDetailRespond.class);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return returnObject;
    }

    /**
     * 15提交测试报告
     * 
     * @param context
     * @param request 对象ReqSubmitTestinfo（详细参考接口文档）
     * @return
     */
    public static SubmitTestinfoRespond getSubmitTestinfo(Context context,
            ArrayList<SubmitTestinfoRequest> request) {
        SubmitTestinfoRespond returnObject = null;

        String returnStr = requsetUtil(request, GET_SUBMIT_TEST_INFO, context);
        if (!returnStr.equals("")) {
            Gson gsonf = new Gson();

            try {
                returnObject = gsonf.fromJson(returnStr, SubmitTestinfoRespond.class);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return returnObject;
    }

    /**
     * 16应用版本更新
     * 
     * @param context
     * @param os 手机平台ID
     * @return
     */
    public static AppUpdateRespond getAppUpdate(Context context) {
        AppUpdateRespond returnObject = null;
        AppUpdateRequest request = new AppUpdateRequest("1");

        String returnStr = requsetUtil(request, GET_APP_UPDATA, context);
        if (!returnStr.equals("")) {
            Gson gsonf = new Gson();
            try {
                returnObject = gsonf.fromJson(returnStr, AppUpdateRespond.class);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        // //测试构造假数据
        // AppUpdate versionInfo =new
        // AppUpdate(15,3,"3.4.12","2013-3-22",HttpTestConstant.TestUrl.sina,"测试数据");
        // returnObject=new
        // AppUpdateRespond(HttpConstant.CODE_SUCCESS,versionInfo);
        return returnObject;
    }

    /**
     * 17 手机号上传
     * 
     * @param context
     * @param IMEI 设备标识
     * @param MSISDN 手机号
     * @return
     */
    public static SubmitTelNumberRespond getsubmitTelNumber(Context context, String IMEI,
            String MSISDN) {
        SubmitTelNumberRespond returnObject = null;
        SubmitTelNumberRequest request = new SubmitTelNumberRequest(IMEI, MSISDN);

        String returnStr = requsetUtil(request, GET_SUBMIT_TELNUMBER, context);
        if (!returnStr.equals("")) {
            Gson gsonf = new Gson();

            try {
                returnObject = gsonf.fromJson(returnStr, SubmitTelNumberRespond.class);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return returnObject;
    }

    /**
     * 18获取本机终端排名情况
     * 
     * @param context
     * @param imei 设备标识
     * @param netType 网络类型，1代表2G，2代表TD, 3代表WLAN
     * @return
     */
    public static SelfRankRespond getSelfRank(Context context, String imei, int netType) {
        SelfRankRespond returnObject = null;
        SelfRankRequest request = new SelfRankRequest(imei, netType);

        String returnStr = requsetUtil(request, GET_SELFRANK, context);
        if (!returnStr.equals("")) {
            Gson gsonf = new Gson();
            try {
                returnObject = gsonf.fromJson(returnStr, SelfRankRespond.class);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return returnObject;
    }

    /**
     * 获取服务器配置信息。
     * 
     * @param context
     * @param imei
     * @return 有可能会返回null
     */
    public static ServiceConfigRespond getServiceConfig(Context context) {
        ServiceConfigRespond result = null;
        String returnStr = requsetUtil(null, GET_SERVICE_CONFIG, context);
        if (!returnStr.equals("")) {
            Gson gsonf = new Gson();
            try {
                result = gsonf.fromJson(returnStr, ServiceConfigRespond.class);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return result;

    }

    /**
     * 获取某个城市下面的网格数据。
     * 
     * @param context
     * @param request
     * @return 有可能会为空。
     */
    public static CityGridRespond getCityGrid(Context context, CityGridRequest request) {
        CityGridRespond result = null;
        String returnStr = requsetUtil(request, GET_CITY_GRID, context);
        if (!returnStr.equals("")) {
            Gson gsonf = new Gson();
            try {
                result = gsonf.fromJson(returnStr, CityGridRespond.class);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return result;

    }

    /**
     * 登录接口。
     * 
     * @param context
     * @param name 用户名
     * @param password 密码
     * @return
     */
    public static LoginRespond login(Context context, String name, String password,String versionCode,String versionName) {
        LoginRespond result = null;

        try {
            
            TelephonyManager tm = (TelephonyManager) context
                    .getSystemService(context.TELEPHONY_SERVICE);
            String imsi=tm.getSubscriberId();
            
            LoginRequest request = new LoginRequest(MyDes.encryptDES(name, MyDes.KEY),
                    MyDes.encryptDES(password, MyDes.KEY),versionCode,versionName,imsi);
            String returnStr = requsetUtil(request, LOGIN, context);
            if (!returnStr.equals("")) {
                Gson gsonf = new Gson();
                try {
                    result = gsonf.fromJson(returnStr, LoginRespond.class);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    private static String requsetUtil(Object request, String moth, Context context) {
        try {
            Gson gson = new GsonBuilder().create();
            String requestPackage = gson.toJson(request);
            String imdiMd5 = MyCommonUtil.getDeviceByMD5(MyCommonUtil.getDeviceId(context));
            Message message = HttpRequest.doPost(BASE_URI + File.separator + moth, requestPackage,
                    imdiMd5);
            if (message != null && HttpStatus.SC_OK == message.arg1) {
                return message.obj.toString();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    private static final String RequMsg = "data";

    private static String getData(String json, String data) {
        try {
            return String.valueOf(new JSONObject(json).getJSONObject(data));
        } catch (JSONException e) {
            e.printStackTrace();
            return String.valueOf(new JSONObject());
        }
    }

    private static Gson getGson() {
        Gson gson = new GsonBuilder()
        // .excludeFieldsWithoutExposeAnnotation() // 不导出实体中没有用@Expose注解的属性
                .enableComplexMapKeySerialization() // 支持Map的key为复杂对象的形式
                .serializeNulls().setDateFormat("yyyy-MM-dd HH:mm:ss:SSS")// 时间转化为特定格式
                // .setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE)//
                // 会把字段首字母大写,注:对于实体上使用了@SerializedName注解的不会生效.
                .setPrettyPrinting() // 对json结果格式化.
                // .setVersion(1.0) //
                // 有的字段不是一开始就有的,会随着版本的升级添加进来,那么在进行序列化和返序列化的时候就会根据版本号来选择是否要序列化.
                // @Since(版本号)能完美地实现这个功能.还的字段可能,随着版本的升级而删除,那么
                // @Until(版本号)也能实现这个功能,GsonBuilder.setVersion(double)方法需要调用.
                .create();

        return gson;
    }

}
